#include <stdio.h>
#include <stdlib.h>

typedef struct _Node
{
	int v;
	int flag;
	struct _Node* pNext;
}Node;

Node* g_pHeader;

void append_node(Node* pNode)
{
	pNode->pNext = g_pHeader;
	g_pHeader = pNode;
}

Node* reverse_node(Node* pHeader)
{
	Node* p1;
	Node* p2;
	Node* p3;

	p1 = NULL;
	p2 = pHeader;
	if (p2 == NULL)
	{
		return NULL;
	}
	p3 = p2->pNext;

	while (p2 != NULL)
	{
		p3 = p2->pNext;
		p2->pNext = p1;
		p1 = p2;
		p2 = p3;
	}
	return p1;
}

void sweep_node(Node** ppNode)
{
	Node* pDelete;

	while (*ppNode != NULL)
	{
		if ((*ppNode)->flag)
		{
			pDelete = *ppNode;
			(*ppNode) = pDelete->pNext;
			free(pDelete);
		}
		else
		{
			ppNode = &((*ppNode)->pNext);
		}
	}
}


#define MAX_NODE_COUNT 0x04

void dump(Node* pNode)
{
	while (pNode)
	{
		printf("%d\n", pNode->v);
		pNode = pNode->pNext;
	}
}

int main()
{
	int i;
	Node* pNode;

	g_pHeader = NULL;
	for (i = 0; i < MAX_NODE_COUNT; i += 1)
	{
		pNode = (Node*)malloc(sizeof(Node));
		pNode->v = i;
		if (i % 2 == 0)
		{
			pNode->flag = 1;
		}
		else
		{
			pNode->flag = 0;
		}
		append_node(pNode);
	}

	dump(g_pHeader);
	g_pHeader = reverse_node(g_pHeader);
	dump(g_pHeader);

	sweep_node(&g_pHeader);

	dump(g_pHeader);


	return 0;
}
